﻿using System;
using System.Linq;
using Kiva7.ViewModels;
using Kiva7.DataClasses;
using Kiva7.DataRequests;

namespace Kiva7
{
    public class FriendViewModel : BaseViewModel
    {

        public FriendViewModel()
        {
            _Friends = new LenderCollection();
        }

        public bool IsLoaded
        {
            get { return _isLoaded; }
            set
            {
                if (_isLoaded != value)
                {
                    _isLoaded = value;
                    NotifyPropertyChanged("IsLoaded");
                }
            }
        }

        private LenderRequest lenderRequest;
        internal void Reload()
        {
            _Friends.Clear();
            if (lenderRequest != null)
            {
                lenderRequest.Cancel();
            }
            else
            {
                lenderRequest = new LenderRequest();
                lenderRequest.OnResponse += new DataRequest<DataResponse.LendersResponse, Lender>.DataRequestDelegate(LenderLoaded);
            }

            FriendFile ff = FriendFile.Load();


            _FriendList = ff.Friends;
            //_FriendList.Add(new Friend() { LenderId = "Pieter1984" });
            //_FriendList.Add(new Friend() { LenderId = "Daan3838" });
            //_FriendList.Add(new Friend() { LenderId = "Dan8767" });

            _FriendLoadIndex = 0;
            LoadNextFriend();
        }

        public bool Add(Friend friend)
        {
            if (DoAdd(friend))
            {
                FriendFile ff = new FriendFile(_FriendList);
                ff.Save();
                Reload();
                return true;
            }
            return false;
        }

        private bool DoAdd(Friend friend)
        {
            foreach (Friend f in _FriendList)
            {
                if (string.Compare(friend.LenderId, f.LenderId, StringComparison.CurrentCultureIgnoreCase) == 0)
                {
                    return false;
                }
            }

            _FriendList.Add(friend);

            return true;
        }



        private Friend FindFriend(string lenderId)
        {
            return _FriendList.FirstOrDefault(i => string.Compare( i.LenderId, lenderId, StringComparison.CurrentCultureIgnoreCase) == 0);
        }

        public bool DoDelete(string lenderId)
        {
            _FriendList.Remove(FindFriend(lenderId));
            return true;
        }
        public bool Delete(string lenderId)
        {
            if (DoDelete(lenderId))
            {
                FriendFile ff = new FriendFile(_FriendList);
                ff.Save();
                Reload();
                return true;
            }
            return false;
        }

        public bool Edit(string lenderId, Friend friend)
        {
            if (DoDelete(lenderId))
            {
                if (DoAdd(friend))
                {
                    FriendFile ff = new FriendFile(_FriendList);
                    ff.Save();

                    Reload();
                    return true;
                }
            }
            
            return false;
        }

        private FriendList _FriendList;
        private int _FriendLoadIndex;

        private LenderCollection _Friends;
        private bool _isLoaded;

        public LenderCollection Data
        {
            get
            {
                return _Friends;
            }
        }

        private void LoadNextFriend()
        {
            if (_FriendLoadIndex < _FriendList.Count)
            {
                string lenderId = _FriendList[_FriendLoadIndex].LenderId;
                _FriendLoadIndex++;
                lenderRequest.GetLender(lenderId);
            }
            else
            {
                LenderCollection orderedFriends = new LenderCollection(_Friends);
                _Friends.Clear();
                foreach (Lender lender in orderedFriends.OrderByDescending(i => i.LoanCount))
                {
                    _Friends.Add(lender);
                }
                IsLoaded = true;
                NotifyPropertyChanged("Data");
            }
        }

        private void LenderLoaded(object sender, LenderRequest.DataRequestEventArgs e)
        {
            if (e.Cancelled
                || e.Exception != null
                || e.Response == null
                || e.Response.Lenders == null
                || e.Response.Lenders.LenderItems == null
                || e.Response.Lenders.LenderItems.Count == 0)
            {

            }
            else
            {
                foreach (Lender lender in e.Response.Lenders.LenderItems)
                {
                    Data.Add(lender);
                }
            }
            LoadNextFriend();
        }

    }
}
